package com.scorpio.rotatemenu;

import android.graphics.Bitmap;
import android.graphics.Rect;

public class RutateMenuItem {
    //坐标X轴
    private int x;
    //坐标Y轴
    private int y;
    //半径
    private float radius;
    //旋转角度
    private float angle;
    //icon图像
    public Bitmap bitmap;
    //item设置的宽度
    private int width;
    //偏移角度
    private float offsetAngle = 0;
    //缩放最大倍数
    private float maxZoom;
    //显示的
    private boolean show = false;
    //顺时针最大角度
    private float maxAngle;

    public RutateMenuItem(float radius, int width, float maxZoom, float maxAngle) {
        this.radius = radius;
        this.width = width;
        this.maxZoom = maxZoom;
        this.maxAngle = maxAngle;
    }

    public void setCentre(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setOffsetAngle(float offsetAngle) {
        this.offsetAngle = offsetAngle;
    }

    /**
     * 设置角度
     *
     * @param angle      当前角度
     * @param slideAngle 旋转角度
     */
    public void setAngle(float angle, float slideAngle) {
        float thisAngle = (angle + slideAngle) % 180;
        if (thisAngle < 0) {
            thisAngle = 180 + thisAngle;
        }
        if (thisAngle + offsetAngle > 90) {
            thisAngle = thisAngle + 180;
        }
        this.angle = thisAngle;
    }

    /**
     * @param angle            当前角度
     * @param slideAngle       旋转角度
     * @param slideOffsetAngle 旋转偏移角度
     * @param showStartAngle   显示开始角度
     * @param showEndAngle     显示结束角度
     * @return
     */
    public boolean setAngle(float angle, float slideAngle, float slideOffsetAngle, float showStartAngle, float showEndAngle) {
        float thisAngle = slideOffsetAngle + (angle + slideAngle) % this.maxAngle;
        if (thisAngle < 0) {
            thisAngle = maxAngle + thisAngle;
        }
        if (thisAngle < showStartAngle) {
            thisAngle = maxAngle + thisAngle;
        }
        show = thisAngle >= showStartAngle && thisAngle <= showEndAngle;
        this.angle = thisAngle;
        return show;
    }

    //获取实际X轴
    public int getX() {
        if (angle > 0) {
            return x + (int) (radius * Math.cos((angle + offsetAngle) * Math.PI / 180));
        } else {
            return x + (int) (radius * Math.cos((angle + offsetAngle) * Math.PI / 180));
        }
    }

    //获取实际Y轴
    public int getY() {
        if (angle > 0) {
            return y + (int) (radius * Math.sin((angle + offsetAngle) * Math.PI / 180));
        } else {
            return y + (int) (radius * Math.sin((angle + offsetAngle) * Math.PI / 180));
        }
    }

    //获取item实际宽高
    public float getSize() {
        return width * getZoom();
    }

    //缩放比例
    public float getZoom() {
        float zoom = Math.abs(90f - (angle % 180)) / 90f;
        return (((1 - zoom) / (1f / maxZoom)) + zoom);
    }

    public Rect getSrc() {
        return new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    }

    public Rect getDst() {
        int imageWidth = (int) (getSize() * 2);
        int x = getX();
        int y = getY();
        return new Rect(
                x - imageWidth / 2
                , y - imageWidth / 2
                , x + imageWidth - imageWidth / 2
                , y + imageWidth - imageWidth / 2
        );
    }

    //获取文字x轴
    public int getTextX(int textSize) {
        int imageWidth = (int) (getSize() * 2);
        return getX() + imageWidth / 2 + textSize / 3;
    }

    //获取文字y轴
    public int getTextY(int textSize) {
        return getY() + textSize / 2;
    }

    public float getAngle() {
        return angle;
    }


    public int getLeft() {
        return (int) (getX() - getSize());
    }

    public int getTop() {
        return (int) (getY() - getSize());
    }

    public int getRight() {
        return (int) (getLeft() + getSize() * 2);
    }

    public int getBottom() {
        return (int) (getTop() + getSize() * 2);
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setMaxZoom(float maxZoom) {
        this.maxZoom = maxZoom;
    }

    public boolean isShow() {
        return show;
    }

}
